package com.wntms.yjj.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.ft.pojo.User;
import com.wntms.yjj.dao.StaffManagerDao;
import com.wntms.yjj.pojo.Staff;
import com.wntms.yjj.service.StaffManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class StaffManagerServiceImpl implements StaffManagerService {

    @Autowired
    private StaffManagerDao staffManagerDao;

    public boolean add(Staff staff){
        Integer rid=staff.getRid();
        String name = staff.getName();
        String password = staff.getPassword();
        String acount = staff.getAcount();
        Integer count = staffManagerDao.addU(name, acount, password);
        User user = staffManagerDao.findUid(name);
        staff.setUid(user.getId());
        staffManagerDao.addC(staff);
        Integer uid = user.getId();
        staffManagerDao.setRid(uid,rid);
        return count < 1 ? false : true;
    }

    @Override
    public boolean delete(Integer id) {
//        System.out.println(id);
        Staff staff = staffManagerDao.findById(id);
        staffManagerDao.deleteC(id);
        Integer count = staffManagerDao.deleteU(staff.getUid());
        return count < 1 ? false : true;
    }

    @Override
    public boolean update(Staff staff) {
        Integer uid = staffManagerDao.findById(staff.getId()).getUid();
        System.out.println(uid);
        String name = staff.getName();
        staffManagerDao.updateC(staff);
        Integer count = staffManagerDao.updateU(uid, name);
        return count < 1 ? false : true;
    }

    @Override
    public List<Staff> findAll() {
        List<Staff> list = staffManagerDao.findAll();
        return list;
    }

    @Override
    public Staff findByNum(String num) {
        Staff staff = staffManagerDao.findByNum(num);
        return staff;
    }

    @Override
    public Staff findByName(String name) {
        Staff staff = staffManagerDao.findByName(name);
        return staff;
    }

    @Override
    public List<Staff> findByDepartment(String department) {
        List<Staff> list = staffManagerDao.findByDepartment(department);
        return list;
    }

    @Override
    public List<Staff> findByCondition(Staff staff) {
        List<Staff> list=new ArrayList<Staff>();
        if (staff.getName().length()!=0){
            System.out.println("findByName..."+this.findByName(staff.getName()));
             list.add(this.findByName(staff.getName()));
             return list;
        }
        if(staff.getNum().length()!=0){
            list.clear();
            this.findByDepartment(staff.getDepartment());
            list.add(this.findByNum(staff.getNum()));
            return list;
        }
        if(staff.getDepartment().length()!=0){
            list.clear();
            System.out.println("getDepartment..."+this.findByDepartment(staff.getDepartment()));
            list= this.findByDepartment(staff.getDepartment());
            return list;
        }

            list.clear();
            list = this.findAll();
            return list;

    }

    @Override
    public List<Staff> findByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Staff> list = staffManagerDao.findAll();
        return list;

    }
}
